/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.animatedalligator;

import java.text.SimpleDateFormat;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import uc.edu.server.MessageWithID;

/**
 * Uses a JList for its display surface.
 *
 * @author tony
 */
public class JListClientDisplay extends JList
        implements IClientDisplay
{
    private DefaultListModel messageDisplayModel;
        
    /**
     * Initialize the display.
     */
    @Override
    public void initMessageDisplay()
    {
        
        // save this JList's data model in a "convenience" attribute
        // so we don't have to keep calling getModel() every time
        // we want to use it:
        messageDisplayModel = (DefaultListModel)this.getModel();
        // display a starting message:
        appendString("No messages sent or received yet by this client.");
    }

    /**
     * Removes messages from the display only. Does not affect any "backing
     * store" the program might have outside of this class.
     */
    @Override
    public void clearMessageDisplay()
    {
        messageDisplayModel.clear();
    }

    /**
     * Adds a single message to the message display pane. If this is the first
     * message, this method clears the "no messages yet..." message from the
     * display pane.
     *
     * @param msg
     */
    public void appendMessage(MessageWithID msg)
    {
        if (messageDisplayModel.isEmpty())
          {
            clearMessageDisplay();
          }
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        String s = formatter.format(msg.getTimestamp()) + ": "
                + String.format("%-9.9s", msg.getSender().getUserName()) + " "
                + msg.getContent();
        appendString(s);
    }

    @Override
    public void appendString(String str)
    {
        messageDisplayModel.addElement(str);
    }
}
